import electron.Clipboard;
import electron.main.App;
import electron.main.IpcMain;
import electron.main.BrowserWindow;
import electron.main.MenuItem;
import electron.main.Menu;

@:expose
class ElectronTools {
	static function main() {}

	public static var previewWindow:BrowserWindow;

	public static function init(window:BrowserWindow):Void {
		var menu = new Menu();
		menu.append(new MenuItem({
			label: "应用",
			submenu: [
				{
					label: "关于",
					click: function() {
						untyped window.send("about");
					}
				},
				{
					label: "设置",
					click: function() {
						untyped window.send("settings");
					}
				},
				{
					label: "刷新",
					click: function() {
						window.reload();
					}
				},
				{
					label: "更新版本",
					click: function() {
						untyped window.send("check-update");
					}
				},
				{
					label: "关闭",
					accelerator: "CmdOrCtrl+W",
					click: function() {
						if (previewWindow != null) {
							try {
								previewWindow.close();
							} catch (e) {}
							previewWindow = null;
						}
					}
				}
			]
		}));
		menu.append(new MenuItem({
			label: "文件",
			submenu: [
				{
					label: "打开",
					accelerator: "CmdOrCtrl+O",
					click: function() {
						untyped window.send("open-project");
					}
				},
				{
					label: "新建人物工程",
					accelerator: "CmdOrCtrl+N",
					click: function() {
						untyped window.send("new-project");
					}
				},
				{
					label: "保存",
					accelerator: "CmdOrCtrl+S",
					click: function() {
						untyped window.send("save");
					}
				},
				{
					label: "另保存...",
					accelerator: "Alt+CmdOrCtrl+S",
					click: function() {
						untyped window.send("saveAs");
					}
				},
				{
					label: "调试",
					accelerator: "F12",
					click: function() {
						if (previewWindow != null && !previewWindow.isDestroyed()) {
							previewWindow.webContents.openDevTools();
						} else {
							window.webContents.openDevTools();
						}
					}
				}
			]
		}));

		menu.append(new MenuItem({
			label: "编辑",
			submenu: [
				{
					label: "添加特效",
					accelerator: "CmdOrCtrl+E",
					click: function() {
						untyped window.send("add-effect");
					}
				},
				{
					label: "人物属性",
					accelerator: "CmdOrCtrl+M",
					click: function() {
						untyped window.send("edit-character");
					}
				},
				{
					label: "编辑特效锚点",
					accelerator: "F8",
					click: function() {
						untyped window.send("edit-effect-anchor");
					}
				},
				{
					label: "时间轴",
					submenu: [
						{
							label: "克隆帧",
							accelerator: "F6",
							click: function() {
								untyped window.send("clone-frame");
							}
						},
						{
							label: "空白帧",
							accelerator: "F7",
							click: function() {
								untyped window.send("blank-frame");
							}
						},
						{
							label: "位移缩放",
							click: function() {
								untyped window.send("timeline-move-scale");
							}
						},
						untyped {
							type: 'separator'
						},
						{
							label: "添加位移帧",
							accelerator: "F1",
							click: function() {
								untyped window.send("add-move-frame");
							}
						},
						{
							label: "添加缩放帧",
							accelerator: "F2",
							click: function() {
								untyped window.send("add-scale-frame");
							}
						},
						{
							label: "添加旋转帧",
							accelerator: "F3",
							click: function() {
								untyped window.send("add-rotate-frame");
							}
						},
						{
							label: "添加透明帧",
							accelerator: "F4",
							click: function() {
								untyped window.send("add-alpha-frame");
							}
						}
					]
				},
				{type: 'separator'},
				{
					label: "剪切",
					role: 'cut'
				},
				{
					label: "复制",
					accelerator: "CmdOrCtrl+C",
					click: function() {
						untyped window.send("copy");
						window.webContents.copy();
					}
				},
				{
					label: "粘贴",
					accelerator: "CmdOrCtrl+V",
					click: function() {
						untyped window.send("paste");
						window.webContents.paste();
					}
				},
				{
					label: "撤销",
					accelerator: 'CmdOrCtrl+Z',
					click: function() {
						untyped window.send("undo");
					}
				},
				{type: 'separator'},
				{
					label: "全选",
					role: 'selectall'
				}
			]
		}));

		menu.append(new MenuItem({
			label: "构造",
			submenu: [
				{
					label: "预览调试",
					accelerator: "CmdOrCtrl+R",
					click: function() {
						untyped window.send("role-build-preview");
					}
				},
				{
					label: "编译",
					accelerator: "CmdOrCtrl+B",
					click: function() {
						untyped window.send("role-build");
					}
				},
			]
		}));

		menu.append(new MenuItem({
			label: "工具",
			submenu: [
				{
					label: "初始化动作",
					click: function() {
						untyped window.send("role-init-action");
					}
				},
				{
					label: "初始化代码",
					click: function() {
						untyped window.send("role-init-code");
					}
				},
				{
					label: "初始化出招表",
					click: function() {
						untyped window.send("role-init-action-table");
					}
				},
				{type: 'separator'},
				{
					label: "精灵图去重",
					click: function() {
						untyped window.send("atlas-delete-duplicate");
					}
				},
				{
					label: "旧版本转换（.data）",
					click: function() {
						untyped window.send("role-old-version-convert");
					}
				},
				{
					label: "旧版本转换（.xml）",
					click: function() {
						untyped window.send("role-old-version-convert-xml");
					}
				}
			]
		}));

		Menu.setApplicationMenu(menu);

		IpcMain.on("runtime-preview", (event, data) -> {
			trace("启动角色预览", data);
			if (previewWindow != null) {
				try {
					previewWindow.close();
				} catch (e) {}
				previewWindow = null;
			}
			var window = new BrowserWindow({
				title: "角色预览",
				width: 1080,
				height: 610,
				resizable: false,
				webPreferences: {
					nodeIntegration: true
				}
			});
			window.loadURL("file://" + js.Node.__dirname + "/runtime/index.html?abn=" + data);
			window.show();
			// window.webContents.openDevTools();
			previewWindow = window;
		});

		IpcMain.on("relunch", (event, data) -> {
			// 整个应用重启
			App.relaunch();
			App.exit(0);
		});

		IpcMain.on("change-title", (event, data) -> {
			window.setTitle(data);
		});
	}
}
